package com.yx.modules.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.dao.TbUserMapper;
import com.yx.modules.api.entity.TbUser;
import com.yx.system.dao.TdBookMapper;
import com.yx.system.dao.TdDiscussMapper;
import com.yx.system.dto.DiscussDTO;
import com.yx.system.entity.TdDiscuss;
import com.yx.utils.EntityDtoConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.*;

/**
 * 评论控制器
 *
 * @author xiaoyu
 */
@RestController
@RequestMapping("/api/discuss")
public class TdDiscussController {

    @Autowired
    private TdBookMapper tdBookMapper;

    @Autowired
    private TdDiscussMapper tdDiscussMapper;

    @Autowired
    private TbUserMapper tbUserMapper;

    /**
     * 获取书籍评价列表
     *
     * @param map
     * @return
     */
    @AuthIgnore
    @PostMapping("list")
    public R discuss(@RequestParam Map<String, Object> map) {
        Query query = new Query(map);
        int total = tdDiscussMapper.queryTotal(query);
        List<DiscussDTO> discussDTOList = new ArrayList<>();
        List<TdDiscuss> discussList = tdDiscussMapper.queryList(query);
        for (int i = 0; i < discussList.size(); i++) {
            DiscussDTO dd = new DiscussDTO();
            EntityDtoConverter ed = new EntityDtoConverter();
            try {
                dd = (DiscussDTO) ed.entityConvertDto(discussList.get(i), dd);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            dd.setUserInfo(tbUserMapper.selectByPrimaryKey(Long.parseLong(discussList.get(i).getUserid() + "")));
            discussDTOList.add(dd);
        }
        PageUtils pageUtil = new PageUtils(discussDTOList, total, query.getLimit(), query.getPage());
        return R.ok().put("discussList", pageUtil);
    }


    /**
     * 评价某本书籍
     *
     * @param tbUser   用户信息
     * @param bookId
     * @param content
     * @param pic
     * @param reply
     * @param evaluate
     * @return
     */
    @PostMapping("comment")
    public R comment(@LoginUser TbUser tbUser, int bookId, String content, String pic, int reply, int evaluate) {
        if (tbUser != null) {
            TdDiscuss cots = new TdDiscuss();
            Date date = new Date();
            Timestamp nousedate = new Timestamp(date.getTime());
            cots.setCreatedate(nousedate);
            cots.setBookid(tdBookMapper.selectByPrimaryKey(bookId).getId());
            cots.setContent(content);
            cots.setClickpraise(1);
            cots.setPic(pic);
            cots.setUserid(tbUser.getUserId());
            if (reply > 0) {
                cots.setReply(reply);
            } else {
                cots.setReplyid(0);
            }
            cots.setReply(reply);
            cots.setReplynumber(0);
            cots.setEvaluate(evaluate);
            tdDiscussMapper.insert(cots);
        } else {
            R.error("未查询到用户信息！");
        }
        return R.ok();
    }

    /**
     * 获取某条评论的回复列表
     *
     * @param replyId 评论ID
     * @return
     */
    @AuthIgnore
    @PostMapping("comment/replyList")
    public R reply(Integer replyId) {
        List<TdDiscuss> cts = new ArrayList<>();
        List<DiscussDTO> discussDTOList = new ArrayList<>();
        cts = tdDiscussMapper.findByReplyId(replyId);
        for (int i = 0; i < cts.size(); i++) {
            DiscussDTO dd = new DiscussDTO();
            EntityDtoConverter ed = new EntityDtoConverter();
            try {
                dd = (DiscussDTO) ed.entityConvertDto(cts.get(i), dd);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            dd.setUserInfo(tbUserMapper.selectByPrimaryKey(Long.parseLong(cts.get(i).getUserid() + "")));
            discussDTOList.add(dd);
        }
        return R.ok().put("cts", discussDTOList);
    }

    /**
     * 回复某条评论
     *
     * @param id      评论ID
     * @param content 内容
     * @return
     */
    @PostMapping("comment/reply")
    public R reply(@LoginUser TbUser tbUser, Integer id, String content) {
        TdDiscuss cts = tdDiscussMapper.selectByPrimaryKey(id);
        if (tbUser != null) {
            if (cts != null) {
                TdDiscuss td = new TdDiscuss();
                Date date = new Date();
                Timestamp nousedate = new Timestamp(date.getTime());
                td.setCreatedate(nousedate);
                td.setContent(content);
                td.setReplyid(id);
                td.setReply(1);
                td.setUserid(tbUser.getUserId());
                td.setReplynumber(0);
                cts.setReplynumber(cts.getReplynumber() + 1);
                tdDiscussMapper.updateByPrimaryKey(cts);
                tdDiscussMapper.insert(td);
            } else {
                R.error("未查询到评价信息!");
            }
        } else {
            R.error("未查询到用户信息!");
        }
        return R.ok();
    }


    /**
     * 单个评论详情
     *
     * @param id 评论ID
     * @return
     */
    @PostMapping("comment/ById")
    public R ById(Integer id) {
        TdDiscuss discuss = tdDiscussMapper.selectByPrimaryKey(id);
        return R.ok().put("discuss", discuss);
    }

    /**
     * 单个评论回复列表
     *
     * @param id 评论ID
     * @return
     */
    @PostMapping("replay")
    public R replay(Integer id) {
        List<TdDiscuss> tdDiscussList = tdDiscussMapper.findByReplyId(id);
        return R.ok().put("tdDiscussList", tdDiscussList);
    }
}
